﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Reflection;

namespace MvcTestApp.Utility
{
    public class SessionCache
    {
        public static String CurrentUser
        {
            get
            {                
                return HttpContext.Current.Session["asdsd"] as String;
            }
            set
            {
                HttpContext.Current.Session["asdsd"] = value;
            }
        }

        public static SelectList GetSelectList<T>(List<T> items, String displayField, String valueField) where T : class
        {
            Type type = typeof(T);
            object obj = Activator.CreateInstance(type);
            PropertyInfo displayProperty = type.GetProperty(displayField);
            if(displayProperty == null)
                throw new ArgumentException(String.Format("Specified Display Field Property {0} was not found.", displayField));
            else
                displayProperty.SetValue(obj, "Please Select", null);
            
            displayProperty = type.GetProperty(valueField);
            if(displayProperty == null)
                throw new ArgumentException(String.Format("Specified Value Field Property {0} was not found.", valueField));
            else
                displayProperty.SetValue(obj, "0", null);            
            
            items.Add((T)obj);
            SelectList selectList = new SelectList(items);
            return selectList;
        }
    }
}